﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows.Controls;
using Microsoft.WVB.Framework.Entities;
using Microsoft.WVB.Silverlight.Infrastructure;
using Microsoft.WVB.Framework;

namespace Microsoft.WVB.UI
{
    public class EditForm : FormPrimitive
    {
        private PasswordBox passwordBox;
        private PasswordBox passwordConfirmationBox;
        
        public EditForm()
            : base()
        {
            this.DefaultStyleKey = typeof(EditForm);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ContentControl interactiveContentControl = this.GetTemplateChild("ElementInteractive") as ContentControl;

            if (interactiveContentControl != null)
            {
                // Pull the password box out of the ContentControl. Binding is not allowed on a PasswordBox
                this.passwordBox = ((ScrollViewer)interactiveContentControl.Content).FindName("SignUpPassword") as PasswordBox;
                this.passwordConfirmationBox = ((ScrollViewer)interactiveContentControl.Content).FindName("SignUpRetypedPassword") as PasswordBox;
            }
        }

        protected override void OnButtonSubmitClicked(Button submitButton)
        {
            submitButton.IsEnabled = false;

            if ((!String.IsNullOrEmpty(ViewModel.User.Password)) &&
                (!String.IsNullOrEmpty(ViewModel.User.RetypedPassword)) &&
                (!String.IsNullOrEmpty(ViewModel.User.PPID)) &&
                (!String.IsNullOrEmpty(ViewModel.User.Email)) &&
                (ViewModel.User.Password.Equals(ViewModel.User.RetypedPassword, StringComparison.CurrentCulture)))
            {
                ViewModel.Update();
            }
            else
            {
                ViewModel.SetUserState(UserManagerState.Failed, Strings.EditForm_NullArguments);
            }

            submitButton.IsEnabled = true;
        }

        protected override void OnCurrentStateChanged(UserManagerState oldValue, UserManagerState newValue)
        {
            if (newValue == UserManagerState.Signed)
            {
                if (ButtonReset != null)
                    ButtonReset.Visibility = System.Windows.Visibility.Collapsed;

                if (ButtonSubmit != null)
                    ButtonSubmit.Visibility = System.Windows.Visibility.Visible;
            }
            else if (newValue == UserManagerState.Failed)
            {

                if (ButtonReset != null)
                    ButtonReset.Visibility = System.Windows.Visibility.Visible;

                if (ButtonSubmit != null)
                    ButtonSubmit.Visibility = System.Windows.Visibility.Collapsed;

            }
            else if (newValue == UserManagerState.Updated)
            {
                if (ButtonSubmit != null)
                    ButtonSubmit.Visibility = System.Windows.Visibility.Collapsed;

                if (ButtonReset != null)
                    ButtonReset.Visibility = System.Windows.Visibility.Collapsed;
            }

            base.OnCurrentStateChanged(oldValue, newValue);
        }

        protected override void OnButtonCancelClicked(Button cancelButton)
        {
            //CommandService.NotifyWindow("CLOSE_EditProfileUI", this);
            CommandService.NotifyWindow("EditProfileUI",  WindowCommandEventArg.WindowCommand.Close);

            if ((this.CurrentState == UserManagerState.Failed) || (this.CurrentState == UserManagerState.Updated))
                ViewModel.SetUserState(UserManagerState.Signed);
        }
        protected override void ChangeVisualState(bool useTransitions)
        {
            if (this.CurrentState == UserManagerState.Signed)
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateFormDisplayed });
            }

            if (this.CurrentState == UserManagerState.Updating)
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateFormSubmitted });
            }

            if ((this.CurrentState == UserManagerState.Failed) || (this.CurrentState == UserManagerState.Updated))
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateFormNotified });
            }
        }

        protected override void OnButtonResetClicked(Button resetButton)
        {
            ViewModel.SetUserState(UserManagerState.Signed);
        }
    }
}
